package com.hsc.www.integration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RegisterMessageGateway registerMessageGateway;


    // 返回所有的用户
    public List<User> listUsers() {
        return (List<User>) userRepository.findAll();
    }

    // 保存用户
    public User saveUser(User user) {
        // 保存数据
        userRepository.save(user);
        // 调用注册通知流程
        registerMessageGateway.registerMessageFlow(user);

        return user;
    }


    // 删除用户
    public void deleteUser(Long id) {
        userRepository.deleteById(id);
    }

    // 查找用户
    public User findUser(Long id) {
        return userRepository.findById(id).get();
    }

    // 根据名称查找用户
    public List<User> searchUser(String name) {
        return userRepository.findByName(name);
    }

}
